package cn.studyjams.s1.contest.autohello;

import android.app.ActivityOptions;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import java.util.ArrayList;
import java.util.List;

import cn.studyjams.s1.contest.autohello.adapter.EventCardAdapter;
import cn.studyjams.s1.contest.autohello.model.EventCard;

public class EventCardFragment extends Fragment {

    private final static String TAG = "EventCardFragment";

    private RecyclerView mRecyclerView;
    private List<EventCard> mDataset;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_event, container, false);

        mRecyclerView = (RecyclerView) view.findViewById(R.id.event_recycler_view);
        mRecyclerView.setHasFixedSize(true);

        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        GridLayoutManager gridLayoutManager = new GridLayoutManager(getActivity(), 2);
        mRecyclerView.setLayoutManager(gridLayoutManager);

        mDataset = initDataset();

        RecyclerView.Adapter adapter = new EventCardAdapter(mDataset) {
            @Override
            public void onBindViewHolder(final EventCardAdapter.ViewHolder holder, final int position) {
                super.onBindViewHolder(holder, position);
                final EventCard eventCard = mDataset.get(position);

                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {

                        if (eventCard.getId() != 4) {
                            Intent intent = new Intent(getActivity(), HelloActivity.class);
                            intent.putExtra("eventType", eventCard.getId());

                            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
                                startActivity(intent);
                            } else {
                                ImageView coverIV = (ImageView) view.findViewById(R.id.event_cover);

                                Bundle bundle = ActivityOptions
                                        .makeSceneTransitionAnimation(
                                                getActivity(),
                                                coverIV,
                                                coverIV.getTransitionName()
                                        ).toBundle();
                                startActivity(intent, bundle);
                            }
                        } else {
                            Snackbar.make(view, getString(R.string.more_event_desc), Snackbar.LENGTH_SHORT).show();
                        }
                    }
                });
            }
        };
        mRecyclerView.setAdapter(adapter);

    }

    public List<EventCard> initDataset() {
        List<EventCard> dataset = new ArrayList<>();

        List<Drawable> drawables = new ArrayList<>();
        drawables.add(ContextCompat.getDrawable(getActivity(), R.drawable.ic_hello));
        drawables.add(ContextCompat.getDrawable(getActivity(), R.drawable.ic_sunny));
        drawables.add(ContextCompat.getDrawable(getActivity(), R.drawable.ic_birthday));
        drawables.add(ContextCompat.getDrawable(getActivity(), R.drawable.ic_holiday));
        drawables.add(ContextCompat.getDrawable(getActivity(), R.drawable.ic_plus));


        String[] titles = new String[]{
                getResources().getString(R.string.custom_hello),
                getResources().getString(R.string.weather_cast),
                getResources().getString(R.string.happy_birthday),
                getResources().getString(R.string.festival_blessing),
                getResources().getString(R.string.more_event)
        };

        String[] descs = new String[]{
                getResources().getString(R.string.custom_hello_desc),
                getResources().getString(R.string.weather_cast_desc),
                getResources().getString(R.string.happy_birthday_desc),
                getResources().getString(R.string.festival_blessing_desc),
                getResources().getString(R.string.more_event_desc)
        };

        for (int i = 0; i < drawables.size(); i++) {
            EventCard EventCard = new EventCard(
                    i,
                    drawables.get(i),
                    titles[i],
                    descs[i]
            );
            dataset.add(EventCard);
        }

        return dataset;
    }
}
